<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="车辆编码" prop="vehicleCode">
                <el-input v-model="queryParams.vehicleCode" placeholder="请输入车辆编码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备编码" prop="deviceCode">
                <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="告警ID" prop="alarmId">
                <el-input v-model="queryParams.alarmId" placeholder="请输入告警ID" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备上报时间" prop="machineTime">
                <el-date-picker clearable v-model="queryParams.machineTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择设备上报时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="记录时间" prop="logTime">
                <el-date-picker clearable v-model="queryParams.logTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择记录时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:log:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
            <el-table-column label="ID" align="center" prop="id" v-if="true" />
            <el-table-column label="车辆编码" align="center" prop="vehicleCode" />
            <el-table-column label="用户编码" align="center" prop="userCode" />
            <el-table-column label="设备编码" align="center" prop="deviceCode" />
            <el-table-column label="设备告警经度" align="center" prop="startLongitude" width="150"/>
            <el-table-column label="设备告警纬度" align="center" prop="startLatitude" width="150"/>
            <el-table-column label="告警ID" align="center" prop="alarmId" />
            <el-table-column label="告警名称" align="center" prop="alarmName" />
            <el-table-column label="设备上报时间" align="center" prop="machineTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.machineTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="服务器接收时间" align="center" prop="receiverTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.receiverTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="记录时间" align="center" prop="logTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.logTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row)"
                        v-hasPermi="['system:log:view']">查看告警详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 查看详情 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="车辆编码" prop="vehicleCode">
                    <el-input v-model="form.vehicleCode" placeholder="请输入车辆编码" />
                </el-form-item>
                <el-form-item label="用户编码" prop="userCode">
                    <el-input v-model="form.userCode" placeholder="请输入用户编码" />
                </el-form-item>
                <el-form-item label="设备编码" prop="deviceCode">
                    <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
                </el-form-item>
                <el-form-item label="设备告警开始经度" prop="startLongitude">
                    <el-input v-model="form.startLongitude" placeholder="请输入设备告警开始经度" />
                </el-form-item>
                <el-form-item label="设备告警开始纬度" prop="startLatitude">
                    <el-input v-model="form.startLatitude" placeholder="请输入设备告警开始纬度" />
                </el-form-item>
                <el-form-item label="设备告警开始地理位置" prop="startLocation">
                    <el-input v-model="form.startLocation" placeholder="请输入设备告警开始地理位置" />
                </el-form-item>
                <el-form-item label="告警ID" prop="alarmId">
                    <el-input v-model="form.alarmId" placeholder="请输入告警ID" />
                </el-form-item>
                <el-form-item label="告警名称" prop="alarmName">
                    <el-input v-model="form.alarmName" placeholder="请输入告警名称" />
                </el-form-item>
                <el-form-item label="告警名称-英文" prop="alarmNameEn">
                    <el-input v-model="form.alarmNameEn" placeholder="请输入告警名称-英文" />
                </el-form-item>
                <el-form-item label="设备上报时间" prop="machineTime">
                    <el-date-picker clearable v-model="form.machineTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择设备上报时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="服务器接收时间" prop="receiverTime">
                    <el-date-picker clearable v-model="form.receiverTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择服务器接收时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="记录时间" prop="logTime">
                    <el-date-picker clearable v-model="form.logTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择记录时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="是否在App显示：1否，2是" prop="isAppShow">
                    <el-input v-model="form.isAppShow" placeholder="请输入是否在App显示：1否，2是" />
                </el-form-item>
                <el-form-item label="是否已读：1否，2是" prop="isRead">
                    <el-input v-model="form.isRead" placeholder="请输入是否已读：1否，2是" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listLog, getLog } from "@/api/alarm/data";
export default {
    name: "Log",
    data() {
        return {
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 设备告警记录表格数据
            logList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                vehicleCode: undefined,
                userCode: undefined,
                deviceCode: undefined,
                startLongitude: undefined,
                startLatitude: undefined,
                startLocation: undefined,
                alarmId: undefined,
                alarmName: undefined,
                alarmNameEn: undefined,
                machineTime: undefined,
                receiverTime: undefined,
                logTime: undefined,
                isAppShow: undefined,
                isRead: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                id: [
                    { required: true, message: "ID不能为空", trigger: "blur" }
                ],
                vehicleCode: [
                    { required: true, message: "车辆编码不能为空", trigger: "blur" }
                ],
                userCode: [
                    { required: true, message: "用户编码不能为空", trigger: "blur" }
                ],
                deviceCode: [
                    { required: true, message: "设备编码不能为空", trigger: "blur" }
                ],
                startLongitude: [
                    { required: true, message: "设备告警开始经度不能为空", trigger: "blur" }
                ],
                startLatitude: [
                    { required: true, message: "设备告警开始纬度不能为空", trigger: "blur" }
                ],
                startLocation: [
                    { required: true, message: "设备告警开始地理位置不能为空", trigger: "blur" }
                ],
                alarmId: [
                    { required: true, message: "告警ID不能为空", trigger: "blur" }
                ],
                alarmName: [
                    { required: true, message: "告警名称不能为空", trigger: "blur" }
                ],
                alarmNameEn: [
                    { required: true, message: "告警名称-英文不能为空", trigger: "blur" }
                ],
                machineTime: [
                    { required: true, message: "设备上报时间不能为空", trigger: "blur" }
                ],
                receiverTime: [
                    { required: true, message: "服务器接收时间不能为空", trigger: "blur" }
                ],
                logTime: [
                    { required: true, message: "记录时间不能为空", trigger: "blur" }
                ],
                isAppShow: [
                    { required: true, message: "是否在App显示：1否，2是不能为空", trigger: "blur" }
                ],
                isRead: [
                    { required: true, message: "是否已读：1否，2是不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询设备告警记录列表 */
        getList() {
            this.loading = true;
            listLog(this.queryParams).then(response => {
                this.logList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                vehicleCode: undefined,
                userCode: undefined,
                deviceCode: undefined,
                startLongitude: undefined,
                startLatitude: undefined,
                startLocation: undefined,
                alarmId: undefined,
                alarmName: undefined,
                alarmNameEn: undefined,
                machineTime: undefined,
                receiverTime: undefined,
                logTime: undefined,
                isAppShow: undefined,
                isRead: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 查看详情按钮操作 */
        handleView(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getLog(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改设备告警记录";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.id != null) {
                        updateLog(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addLog(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/log/export', {
                ...this.queryParams
            }, `log_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  